package org.cyhz.standardize.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.cyhz.domain.SbNbVO;
import org.cyhz.domain.SbVO;
import org.cyhz.util.Log;
import org.springframework.stereotype.Service;

@Service("sBrandSearchService")
public class SBrandSearchServiceImp implements SBrandSearchService {

	private Comparator<String> comparator = new Comparator<String>() {
		@Override
		public int compare(String o1, String o2) {
			return o2.length() - o1.length();
		}
	};

	@Override
	public List<SbVO> getSBrandListFromSbNb(String nb) {
		List<SbVO> sbVOs = new ArrayList<>();
		if (StandardizeResources.nbsbMapping.containsKey(nb)) {
			sbVOs = StandardizeResources.nbsbMapping.get(nb);
		}
		return sbVOs;
	}

	@Override
	public List<SbVO> getSBrandListFromSbSs(String ss) {
		List<SbVO> sbVOs = new ArrayList<>();
		if (StandardizeResources.sssbMapping.containsKey(ss)) {
			sbVOs = StandardizeResources.sssbMapping.get(ss);
		}
		return sbVOs;
	}

	@Override
	public List<SbVO> getSBrandListFromTC(String tc, List<String> nbs) {

		List<SbVO> sbvos = new ArrayList<>();

		if (null == nbs || nbs.isEmpty()) {
			nbs = new ArrayList<>(StandardizeResources.nbsbMapping.keySet());
		}

		// Entry<SsNsVO.toString,SsNsVO>
		Map<String, SbNbVO> nb_sbnbMapping = new HashMap<>();
		for (String nb : nbs) {
			if (StandardizeResources.nb_sbnbMapping.containsKey(nb)) {
				List<SbNbVO> sbnbvos = StandardizeResources.nb_sbnbMapping
						.get(nb);
				for (SbNbVO sbNbVO : sbnbvos) {
					nb_sbnbMapping.put(sbNbVO.toString(), sbNbVO);
				}
			}
		}

		// 非标准品牌只对应一个标准品牌
		// Entry<SbNbVO.n,SbNbVO>
		Map<String, SbNbVO> suit_nbname_sbnbMapping = new HashMap<>();
		for (SbNbVO sbnbvo : nb_sbnbMapping.values()) {
			suit_nbname_sbnbMapping.put(sbnbvo.getN(), sbnbvo);
		}
		// finish get suit nbs

		Map<String, SbNbVO> containsNbName_SbNbMapping = new HashMap<>();
		List<String> nbNames = new ArrayList<>(suit_nbname_sbnbMapping.keySet());
		// 按长度降序
		// List<String> lengthsortedNbName = Lambda.sort(nbNames,
		// Lambda.on(String.class).length());

		List<String> lengthsortedNbName = new ArrayList<>(nbNames);
		Collections.sort(lengthsortedNbName, comparator);

		// 保存已经被包含的非标准品牌名，用于过滤后续的被包含的非标准品牌名做验证
		List<String> containsNbNames = new ArrayList<>();
		
		for (String nbname : lengthsortedNbName) {
			if (tc.contains(nbname)) {
				String name = whichContains(containsNbNames, nbname);
				if (StringUtils.isBlank(name)) {
					containsNbName_SbNbMapping.put(nbname,
							suit_nbname_sbnbMapping.get(nbname));
					containsNbNames.add(nbname);
					Log.info(tc + " contains nb " + nbname );
				} else {
					Log.info(name + " contains nb " + nbname + ",ignore " + nbname);
				}
			}
		}

		List<SbNbVO> sbnbs = new ArrayList<>(
				containsNbName_SbNbMapping.values());

		Map<String, SbVO> sbname_sbvoMap = new HashMap<>();
		for (SbNbVO sbNbVO : sbnbs) {
			sbname_sbvoMap.put(sbNbVO.getS(),
					StandardizeResources.sbName_sbvoMapping.get(sbNbVO.getS()));
		}
		sbvos.addAll(sbname_sbvoMap.values());
		return sbvos;
	}

	private String whichContains(List<String> lengther, String nb) {
		for (String s : lengther) {
			if (s.contains(nb)) {
				return s;
			}
		}
		return null;
	}

}
